package m

type BaseIdKV struct {
	Id  int64 `json:"id"`
	Key int64 `json:"key"`
	Val int64 `json:"val"`
}

type BaseKV struct {
	Key int64 `json:"key"`
	Val int64 `json:"val"`
}

type UpdateProp struct {
	Kvs            []*BaseKV      `json:"kvs"`
	HeroGoodsList  []*HeroGoods   `json:"heroGoodsList"`
	HeroUprops     []*HeroUprop   `json:"heroUprops"`
	Pets           []*UpdatePet   `json:"pets"`
	Petinfo        *UpdatePetInfo `json:"petinfo"`
	slotNoticeSkip bool
}

type HeroUprop struct {
	Tid     int64 `json:"tid"`
	SkillId int64 `json:"skillId"`
}

type UpdatePetInfo struct {
	ExpNum    int64 `json:"expNum"`
	FoodNum   int64 `json:"foodNum"`
	SelectTid int64 `json:"selectTid"`
}

type UpdatePet struct {
	Tid         int64 `json:"tid"`
	Lv          int64 `json:"lv"`
	Exp         int64 `json:"exp"`
	Step        int64 `json:"step"`
	SkillVal    int64 `json:"skillVal"`
	LiveEndTime int64 `json:"liveEndTime"`
	Status      int64 `json:"status"`
}

func NewUpdateProp() *UpdateProp {
	return &UpdateProp{
		Kvs:           make([]*BaseKV, 0),
		HeroGoodsList: make([]*HeroGoods, 0),
		HeroUprops:    make([]*HeroUprop, 0),
		Pets:          make([]*UpdatePet, 0),
	}
}

func (uProp *UpdateProp) MergeKvs() {
	m := make(map[int64]int64, 0)
	for _, v := range uProp.Kvs {
		if val, ok := m[v.Key]; ok {
			m[v.Key] = v.Val + val
			continue
		}
		m[v.Key] = v.Val
	}
	uProp.Kvs = make([]*BaseKV, 0)
	for k, v := range m {
		uProp.Kvs = append(uProp.Kvs, &BaseKV{Key: k, Val: v})
	}
	return
}

// exKeys过滤掉合并的值
func (uProp *UpdateProp) MergeKvs_Except(exKeys ...int64) {
	mExKey := make(map[int64]bool, 0)
	for _, val := range exKeys {
		mExKey[val] = true
	}

	m := make(map[int64]int64, 0)
	ex := make([]*BaseKV, 0)
	for _, v := range uProp.Kvs {
		if _, ok := mExKey[v.Key]; ok {
			ex = append(ex, v)
			continue
		}
		// 未过滤的掉的key，val直接相加。
		if val, ok := m[v.Key]; ok {
			m[v.Key] = v.Val + val
			continue
		}
		m[v.Key] = v.Val
	}
	uProp.Kvs = make([]*BaseKV, 0)
	for k, v := range m {
		uProp.Kvs = append(uProp.Kvs, &BaseKV{Key: k, Val: v})
	}

	uProp.Kvs = append(uProp.Kvs, ex...)
	return
}

func (uProp *UpdateProp) Merge(props ...*UpdateProp) {
	for _, item := range props {
		uProp.Kvs = append(uProp.Kvs, item.Kvs...)
		uProp.HeroGoodsList = append(uProp.HeroGoodsList, item.HeroGoodsList...)
		uProp.HeroUprops = append(uProp.HeroUprops, item.HeroUprops...)
		uProp.Pets = append(uProp.Pets, item.Pets...)
	}
}

func (uProp *UpdateProp) SetSlotNoticeSkip(req bool) {
	uProp.slotNoticeSkip = req
}

func (uProp *UpdateProp) SlotNoticeSkip() bool {
	return uProp.slotNoticeSkip
}
